package cn.piesat.scanning.business.xz.service.gseimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.piesat.scanning.business.xz.service.BuildAlgorithmTaskService;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskPathParam;
import cn.piesat.scanning.business.xz.single.AlgorithmTaskRuleParam;
import cn.piesat.scanning.dto.HtScanningMetadataDTO;
import cn.piesat.scanning.service.HtScanningMetadataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author Ming
 * @version 1.0
 * @description: TODO
 * @date 2023/12/18 13:48
 */
@Service
public class BuildTibet_Grass_RCPServiceImpl implements BuildAlgorithmTaskService {

    @Autowired
    private HtScanningMetadataService htScanningMetadataService;
    @Override
    public Map<String, Object> initAlgorithmParams(String adsType, String primaryFile, String filePath, AlgorithmTaskRuleParam taskRuleParam, AlgorithmTaskPathParam taskPathParam, Map<String, Integer> indexParams, String fileDate, String scanningPath,HtScanningMetadataDTO htScanningMetadataDTO) {
        Map<String, Object> algorithmParams = new HashMap<>();
//天擎_气象要素_影响评估
        String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        String[] s = fileName.split("_");
        String[] s1 = fileName.split("_");
        String level = s1[3];
        String satellite = s1[4];
        String region = s1[2];
        String startTime = s1[9]; //SURF_CHN_MUL_DAY_20220101000000.json
        String endTimeDateStr = s1[10];
        String productMode = s1[8];
        List<String> productList = Stream.of("NDVI", "EVI", "LAI", "FVC", "GPP", "NPP", "NEP").collect(Collectors.toList());
        List<HtScanningMetadataDTO> metadataDTOList = htScanningMetadataService.findListLikeProductMode(startTime, level, satellite,
                productList, region, productMode.substring(2,4), "GSE", "TIFF");
        List<String> collect = metadataDTOList.stream().map(x -> taskPathParam.getParameter02() + File.separator + x.getFilePath()).collect(Collectors.toList());
        String join = StrUtil.join(",", collect);
        List<HtScanningMetadataDTO> obsFileList = htScanningMetadataService.findQxSite(startTime, endTimeDateStr, "DAY", "FY3D",
                "SURF", "CHN",  "JSON");
        List<String> obsCollect = obsFileList.stream().map(x -> "/mnt/sfs/data" + File.separator + x.getFilePath()).collect(Collectors.toList());
        String joinObs = StrUtil.join(",", obsCollect);
        Map<String, Object> params = new HashMap<>();
        if (obsFileList.size() == 0) {
            algorithmParams.put("parameter01", null);
        } else {
            params.put("primaryFile", join);
            params.put("obsFile", joinObs);
            params.put("auxPath", taskPathParam.getParameter03());
            algorithmParams.put("parameter00", adsType);
            algorithmParams.put("parameter01", params);
        }
        return algorithmParams;
    }
}
